CHARTS

Sunniest cities in the world

Bar Polar Grouped

Photo by Donald Giannatti D on Unsplash

Photo by Donald Giannatti D on Unsplash

Yuma, Arizona

You are my Sunshine…
— Johnny Cash


Load the data

Where are the sunniest cities in the world?

# Load data
df <- read_csv("archetypes/sunniest_cities.csv", col_names=TRUE)
df

Wrangle

arrange, group, summarize

We create 4 dataframes: label, base, grid, and measures. Each are used with a separate geom to create the final view.

Here’s the dataframe to create the bars, with the addition of the id field used for the x axis.

empty_bar <- 3

df$group <- str_to_title(df$group)
df <- df %>% arrange(group)
df$id <- seq(1, nrow(df))

label_data <- df
number_of_bar <- nrow(label_data)
angle <- 90 - 360 * (label_data$id-0.5) /number_of_bar     # substract 0.5 because the letter must have the angle of the center of the bars. Not extreme right(1) or extreme left (0)
label_data$hjust <- ifelse( angle < -90, 1, 0)
label_data$angle <- ifelse(angle < -90, angle+180, angle)
# label_data

# prepare a data frame for base lines
base_data <- df %>% 
  group_by(group) %>% 
  summarize(start=min(id), end=max(id) - empty_bar) %>% 
  rowwise() %>% 
  mutate(title=mean(c(start, end)))
# base_data

grid_data <- base_data
grid_data$end <- grid_data$end[ c( nrow(grid_data), 1:nrow(grid_data)-1)] + 1
grid_data$start <- grid_data$start - 1
grid_data <- grid_data[-1,]
# grid_data

# Order data:
df = df %>% arrange(id)

# we'll need this for axis labels, etc.
xmax <- max(df$id)

df$id <- as.factor(df$id)
df

Plot the chart

bar, segment, text, and annotations using a polar coordinate system

theme_opts <- theme(
  text = element_text(family = "inconsolata"), 
  legend.position = "none",
  legend.title = element_blank(),
  axis.text = element_blank(),
  axis.line = element_blank(),
  axis.ticks = element_blank(),
  axis.title = element_blank(),
  panel.grid = element_blank(),
  # panel.grid.major = element_line(linetype = "dotted"),
  panel.grid.major = element_blank(),
  panel.grid.minor = element_blank(),
  panel.background = element_rect(fill="white", colour="white"),
  panel.border = element_blank(),
  plot.title = element_text(vjust = 5.0),
  plot.subtitle = element_text(vjust = 5.0),
  plot.margin = unit(c(0.25,0.25,0.25,0.25), "in")
)

# Note that id is a factor. If x is numeric, there is some space between the first bar
v1 <- ggplot(df, aes(x=id, y=value, fill=group)) +
  geom_bar(stat="identity", alpha=0.7) +
  scale_fill_manual(values = paletteer_d("lisa::RupprechtGeiger")) +
  # Add a val=100/75/50/25 lines.
  geom_segment(data=grid_data, aes(x = 0, y = 4000, xend = xmax, yend = 4000), colour = "white", alpha=0.8, size=0.5 , inherit.aes = FALSE ) +
  geom_segment(data=grid_data, aes(x = 0, y = 3000, xend = xmax, yend = 3000), colour = "white", alpha=0.8, size=0.5 , inherit.aes = FALSE ) +
  geom_segment(data=grid_data, aes(x = 0, y = 2000, xend = xmax, yend = 2000), colour = "white", alpha=0.8, size=0.5 , inherit.aes = FALSE ) +
  # Add text showing the value of each 100/75/50/25 lines
  annotate("text", x = rep(xmax,3), y = c(2000, 3000, 4000), label = c("2000 hrs", "3000 hrs", "4000 hrs") , color="black", size=3 , angle=0, fontface="bold", hjust = 0.5, family = "inconsolata") +
  # Add labels on top of each bar
  geom_text(data=label_data, aes(x=id, y=value+100, label=paste0(individual,": ", round(value,digits=0)), hjust=hjust), 
            color="black", fontface="bold", alpha=0.6, size=4, angle= label_data$angle, inherit.aes = FALSE, family = "inconsolata" ) +
  # Add base line information
  # geom_segment(data=base_data, aes(x = start, y = -2000, xend = end, yend = -2000), colour = "black", alpha=0.8, size=0.6 , inherit.aes = FALSE )  +
  geom_text(data=base_data, aes(x = title, y = -500, label=group), colour = "black", alpha=0.8, size=4, fontface="bold", inherit.aes = FALSE, family = "inconsolata") +
  ylim(-2500,5000) +
  coord_polar() +   
  theme_bw() +
  theme_opts

girafe(ggobj = v1, width_svg = 16, height_svg = 16,
      options = list(opts_sizing(rescale = TRUE, width = 0.8)))

References

citations for narrative and data sources

  • Data sources: Wikipedia page - List of cities by sunshine duration, GO